package what.basic.table;
import javax.swing.event.*;
import javax.swing.table.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import what.basic.basiccomponent.*;
public class Jtablemodelevent implements TableModelListener{
	JTable table=null;
	Mytable1 my=null;
	Jtablemodelevent(){
		JFrame f=new Jframe().jframe("",100,100,true);
		my=new Mytable1();
		my.addTableModelListener(this);
		table=new JTable(my);
		table.setPreferredScrollableViewportSize(new Dimension(550,30));
		JScrollPane sp=new JScrollPane(table);
		f.getContentPane().add(sp,BorderLayout.CENTER);
	}
	public static void main(String[] args) {
		new Jtablemodelevent();

	}
	@Override
	public void tableChanged(TableModelEvent e) {
		int row=e.getFirstRow();
		int grade1=((Integer)(my.getValueAt(row,1))).intValue();
		int grade2=((Integer)(my.getValueAt(row,2))).intValue();
		int total=grade1+grade2;
		my.mySetValueAt(new Integer(total), row, 3);
		table.repaint();
	}
}
class Mytable1 extends AbstractTableModel{
	Object[][] st={
			{"wangpeng",new Integer(91),new Integer(100),new Integer(100)}
			,{"zhuxuelian",new Integer(82),new Integer(69),new Integer(100)},
			{"meiting",new Integer(47),new Integer(57),new Integer(100)},
	};
	String[] names={"name","yuwen","shuxue","zongfen"};
	@Override
	public int getColumnCount() {
		// TODO Auto-generated method stub
		return names.length;
	}
	@Override
	public int getRowCount() {
		// TODO Auto-generated method stub
		return st.length;
	}
	@Override
	public Object getValueAt(int row, int col) {
		// TODO Auto-generated method stub
		return st[row][col];
	}
	public String getColumnName(int col){
		return names[col];
	}
	public Class getColumnClass(int c){
		return getValueAt(0,c).getClass();
	}
	public boolean isCellEditable(int rowIndex,int columnIndex){
		return true;
	}
	public void setValueAt(Object value,int row,int col){
		st[row][col]=value;
		fireTableCellUpdated(row,col);
	}
	public void mySetValueAt(Object value,int row,int col){
		st[row][col]=value;
	}
}
